package com.cat.binaryTree;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/lowest-common-ancestor-of-a-binary-search-tree/
 * @create 2025/9/15 19:12
 * @since JDK17
 */

public class Solution44 {
    int min, max;

    TreeNode f(TreeNode root, TreeNode p, TreeNode q) {
        if (root == null || root == p || root == q) {
            return root;
        }
        TreeNode l = null, r = null;
        if (root.val < min) {
            return f(root.right, p, q);
        } else if (root.val > max) {
            return f(root.left, p, q);
        } else {
            return root;
        }
    }

    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        min = Math.min(p.val, q.val);
        max = Math.max(p.val, q.val);
        return f(root, p, q);
    }
}
